package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ems.entity.UserEntity;
import com.ems.mapper.UserMapper;
import com.ems.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.CommonUtil;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-10-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserVo> queryList (UserVo userVo) {
        userVo.setPage ( ( userVo.getPage () - 1 ) * userVo.getLimit () );
        List<UserVo> userVos = userMapper.queryList ( userVo );
        return userVos;
    }

    @Override
    public Integer queryCount (UserVo userVo) {
        Integer count = userMapper.queryCount ( userVo );
        return count;
    }

    /**
     * 请求Vo对象转成实体对象，入库
     * @param userVo
     * @return
     */
    @Override
    public UserVo saveSingle (UserVo userVo) {
        UserEntity userEntity = new UserEntity ();
        BeanUtils.copyProperties ( userVo, userEntity );
        userEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        userEntity.setCreateTime ( new Date () );
        userEntity.setPassword ( CommonUtil.MD5Util ( "123456" ) );
        // 计算年龄
        if ( StringUtils.isNotBlank ( userEntity.getIdCard () ) ) {
            String year = userEntity.getIdCard ().substring ( 6, 10 ); // 截取身份证的年份
            String date = CommonUtil.getDateStrFormat ( new Date (), "yyyy" ); // 当下年份
            userEntity.setAge ( Integer.parseInt ( date ) - Integer.parseInt ( year ) );
        }
        int insert = baseMapper.insert ( userEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        UserVo vo = new UserVo ();
        BeanUtils.copyProperties ( userEntity, vo );
        return vo;
    }

    @Override
    public UserVo querySingle (UserVo userVo) {
        UserEntity userEntity = new UserEntity ();
        BeanUtils.copyProperties ( userVo, userEntity );
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( userVo.getId () ) ) {
            queryWrapper.eq ( "id", userVo.getId () );
        }
        if ( StringUtils.isNotBlank ( userVo.getTelPhone () ) ) {
            queryWrapper.eq ( "telPhone", userVo.getTelPhone () );
        }
        if ( StringUtils.isNotBlank ( userVo.getPassword () ) ) {
            queryWrapper.eq ( "password", CommonUtil.MD5Util ( userVo.getPassword () ) );
        }
        UserEntity user = baseMapper.selectOne ( queryWrapper );
        if ( user == null ) {
            return null;
        }
        UserVo vo = new UserVo ();
        BeanUtils.copyProperties ( user, vo );
        return vo;
    }


    @Override
    public UserVo updateSingle (UserVo userVo) {
        UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( userVo.getName () ) ) {
            updateWrapper.set ( "name", userVo.getName () );
        }
        if ( StringUtils.isNotBlank ( userVo.getTelPhone () ) ) {
            updateWrapper.set ( "telPhone", userVo.getTelPhone () );
        }
        if ( StringUtils.isNotBlank ( userVo.getPassword () ) ) {
            updateWrapper.set ( "password", userVo.getPassword () );
        }
        if ( StringUtils.isNotBlank ( userVo.getSex () ) ) {
            updateWrapper.set ( "sex", userVo.getSex () );
        }
        if ( StringUtils.isNotBlank ( userVo.getRoleId () ) ) {
            updateWrapper.set ( "roleId", userVo.getRoleId () );
        }
        if ( StringUtils.isNotBlank ( userVo.getIdCard () ) ) {
            updateWrapper.set ( "idCard", userVo.getIdCard () );
        }
        if ( StringUtils.isNotBlank ( userVo.getEmail () ) ) {
            updateWrapper.set ( "email", userVo.getEmail () );
        }
        if ( StringUtils.isNotBlank ( userVo.getAddress () ) ) {
            updateWrapper.set ( "address", userVo.getAddress () );
        }
        if ( StringUtils.isNotBlank ( userVo.getHeadImgUrl () ) ) {
            updateWrapper.set ( "headImgUrl", userVo.getHeadImgUrl () );
        }
        if ( StringUtils.isNotBlank ( userVo.getEducationId () ) ) {
            updateWrapper.set ( "educationId", userVo.getEducationId () );
        }
        if ( StringUtils.isNotBlank ( userVo.getMajor () ) ) {
            updateWrapper.set ( "major", userVo.getMajor () );
        }
        if ( StringUtils.isNotBlank ( userVo.getParentId () ) ) {
            updateWrapper.set ( "parentId", userVo.getParentId () );
        }
        if ( userVo.getLastLoginTime () != null ) {
            updateWrapper.set ( "lastLoginTime", userVo.getLastLoginTime () );
        }
        // 计算年龄
        if ( userVo.getIdCard () != null ) {
            String year = userVo.getIdCard ().substring ( 6, 10 ); // 截取身份证的年份
            String date = CommonUtil.getDateStrFormat ( new Date (), "yyyy" ); // 当下年份
            userVo.setAge ( Integer.parseInt ( date ) - Integer.parseInt ( year ) );
            updateWrapper.set ( "age", userVo.getAge () );
        }
        updateWrapper.set ( "changeTime", new Date () );
        updateWrapper.eq ( "id", userVo.getId () );
        int i = baseMapper.update ( null, updateWrapper );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return userVo;
    }

    @Override
    public List<UserVo> getList (UserVo userVo) {
        List<UserVo> userList = userMapper.getUserList ( userVo );
        return userList;
    }
}
